package com.example.demo.component;

import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
import org.springframework.stereotype.Component;


import com.example.demo.context.RouteDefinitionWrapper;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @author sleepforests
 * Created on 2019-07-17
 */
@Component
public class RouteDefinitionRepositoryImpl implements RouteDefinitionRepository {


    @Override
    public Flux<RouteDefinition> getRouteDefinitions() {
        RouteDefinitionWrapper routeDefinitionWrapper = RouteFileStore.get();
        if (routeDefinitionWrapper == null) {
            return Flux.fromIterable(Collections.emptyList());
        }

        List<RouteDefinition> routeDefinitionList = routeDefinitionWrapper.getRouteDefinitionList();
        return Flux.fromIterable(routeDefinitionList);
    }


    @Override
    public Mono<Void> save(Mono<RouteDefinition> route) {
        return Mono.error(new RuntimeException("can not save route."));
    }

    @Override
    public Mono<Void> delete(Mono<String> routeId) {
        return Mono.error(new RuntimeException("can not delete route."));
    }


}
